<?php $url = new Url();?>
<?php

class ThodeController extends Controller
{
    public $layout='column1';
    public $defaultAction = 'login';

    public function actionIndex(){
        if(isset($_POST['tukhoa']))
        {
            $title = $_POST['tukhoa'];
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;            
            $this->pageTitle = "Trang sổ mơ";
            $tk = new AdminDreamBook();
            $data = $tk->getRowsByTitleSomo($title);
            $count = count($data);
            $rows_per_page = 15;            
            if($count % $rows_per_page == 0)
            {
                $totalpage = floor($count/$rows_per_page);
            }
            else
            {
                $totalpage = floor($count/$rows_per_page) + 1;                             
            }

            $begin = ($page - 1)*$rows_per_page;
            $end = $page * $rows_per_page;
            $util = new Paging();
            $data = $tk->getRowsByTitleSomoPaging($title,$begin,$end);
            $page = $util->showPageNavigation($page,$totalpage,'index?');
            $this->render("index",array('data'=>$data,'page'=>$page));       
        }
        else
        {
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;                        
            $tk = new AdminDreamBook();
            $data = $tk->getRowsBySomo();
            $count = count($data);
            $rows_per_page = 15;            
            if($count % $rows_per_page == 0)
            {
                $totalpage = floor($count/$rows_per_page);
            }
            else
            {
                $totalpage = floor($count/$rows_per_page) + 1;                             
            }            
            $begin = ($page - 1)*$rows_per_page;
            $end = $page * $rows_per_page;            
            $util = new Paging();            
            $data = $tk->getRowsBySomoPaging($begin,$end);            
            $page = $util->showPageNavigation($page,$totalpage,'index?');
            $this->render("index",array('data'=>$data,'page'=>$page));
        }
    }

    public function actionCreate(){        
        if(isset($_GET['edit']))
        {         
            $edit = 1;
            $id = $_GET['edit'];
            $model = new AdminDreamBook();
            $data =  $model->getRowsById($id);               

            $this->render("create",array('edit'=>$edit,'data'=>$data));
        }
        else
        {
            $edit = 0;
            $this->render("create",array('edit'=>$edit));   
        }        
    }

    public function actionSubmitDelete(){                            
        $id = $_POST['id'];                                
        $model = new AdminDreamBook();
        $data = $model->deleteRowById($id);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }           

    public function actionSubmitEdit(){                            
        $id = $_POST['id'];                     
        $str = "create?edit=".$id;                        
        echo $str;        
    }  

    public function actionSubmitUpdate(){                 
        $id = $_POST['id'];                                
        $ten = $_POST['ten'];                                
        $so = $_POST['so'];               
        $model = new AdminDreamBook();
        $data = $model->updateRowById($id,$ten,$so);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    } 

    public function actionSubmitInsert(){        
        $ten = $_POST['ten'];                                
        $so = $_POST['so'];               
        $model = new AdminDreamBook();
        $data = $model->insertRow($ten,$so);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }        

    public function actionUpload(){         
        if(isset($_FILES["file_excel"]) && $_FILES["file_excel"]["error"] == 0){
            echo "Upload: " . $_FILES["file_excel"]["name"] . "<br />";
            echo "Type: " . $_FILES["file_excel"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file_excel"]["size"] / 1024) . " Kb<br />";
            echo "Stored in: " . $_FILES["file_excel"]["tmp_name"];
            $excel_file = $_FILES["file_excel"]["tmp_name"];
            Yii::import('application.extensions.vendors.PHPExcel',true);
            $objPHPExcel = new PHPExcel();            
            $objReader = PHPExcel_IOFactory::createReader('Excel5');                              
            $objPHPExcel = $objReader->load($excel_file);
            $number_sheet = $objPHPExcel->getSheetCount();            
            $a = 0;
            for($i=0; $i < $number_sheet; $i++){
                $objPHPExcel->setActiveSheetIndex($i);
                $number_row = $objPHPExcel->getActiveSheet()->getHighestRow();            
                for($j=2;$j<=$number_row;$j++){
                    $title = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();                    
                    $content = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
                    $data = AdminDreamBook::insertRow($title,$content);
                    if($data != 0){
                        $a++;
                    }                    
                }
            }            
            $this->render("index",array('upload'=>"done"));           
        }else{
            $this->render("upload_excel",array('data'=>""));           
        }  
    }
}
